{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 创建数组最简单的办法就是使用array函数。它接受一切序列型 的对象(包括其他数组)，然后产生一个新的含有传入数据的NumPy数 组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6. , 7.5, 8. , 0. , 1. ])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = [6, 7.5, 8, 0, 1]\n",
    "arr1 = np.array(data1)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 嵌套序列(比如由一组等长列表组成的列表)将会被转换为一个 多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]\n",
    "arr2 = np.array(data2)\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndim返回的是数组的维度\n",
    "arr2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 4)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shape表示数组维度大小的元组\n",
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3, 6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 认为np.empty会返回全0数组的想法是不安全的。很多情况下，它返回的都是一些未初始化的垃圾值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-3.10503618e+231, -3.10503618e+231],\n",
       "        [ 3.95252517e-323,  0.00000000e+000],\n",
       "        [ 0.00000000e+000,  0.00000000e+000]],\n",
       "\n",
       "       [[ 0.00000000e+000,  0.00000000e+000],\n",
       "        [ 0.00000000e+000,  0.00000000e+000],\n",
       "        [ 0.00000000e+000,  0.00000000e+000]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty((2, 3, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组创建函数\n",
    "|函数|说明|\n",
    "|:-|:-|\n",
    "|array|将输入的数据(列表，元组，数组或其他序列类型)转换为ndarray。要么推断出dtype，要么显式指定dtype，默认直接复制输入数据|\n",
    "|asarry|将输入转换为ndarray，如果输入本身就是一个ndarray就不进行复制|\n",
    "|arange|类似于内置的range，但返回的是一个ndarray而不是列表|\n",
    "|ones ones_like|根据指定的形状和dtype创建一个全是1的数组，ones_like以另一个数组为参数，并根据其形状和dtype创建一个全是1的数组|\n",
    "|zeros zeros_like|类似ones ones_like，不过产生的全是0|\n",
    "|empty empty_like|创建数组，只分配内存空间不填充任何值|\n",
    "|eye identity|创建一个正方的NxN单位的矩阵，对角线为1，其余为0|"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "3207008acf26a07c58f9163727980777c0d92fa298586eda8a314250b7931cb4"
  },
  "kernelspec": {
   "display_name": "Python 3.7.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
